from elasticsearch.dsl import Document, Integer, Text, analyzer, Keyword, Float, Date

# 定义ik分词器
ik_analyzer = analyzer('ik_max_word')


class CourseDocument(Document):
    """创建课程文档模型"""
    course_id = Integer()
    course_name = Text(analyzer=ik_analyzer)
    course_img = Keyword()
    course_type = Integer()
    price = Float()
    original_price = Float()
    brief = Text(analyzer=ik_analyzer)
    course_level = Integer()
    pub_date = Date()
    status = Integer()
    students = Integer()
    lessons = Integer()
    pub_lessons = Integer()
    teacher_id = Integer()

    audience = Text(analyzer=ik_analyzer)
    course_description = Text(analyzer=ik_analyzer)

    outcomes = Text(analyzer=ik_analyzer)

    created_time = Date()
    updated_time = Date()
    is_deleted = Integer()
    is_show = Integer()
    course_orders = Integer()
    teacher_name = Text(analyzer=ik_analyzer)
    teacher_title = Text(analyzer=ik_analyzer)

    category_ids = Text()
    category_names = Text(analyzer=ik_analyzer)
    nav_ids = Text()
    nav_names = Text(analyzer=ik_analyzer)
    nav_display_names = Text(analyzer=ik_analyzer)

    class Index:
        name = 'courses'  # 索引名称
        settings = {
            'number_of_shards': 1,
            'number_of_replicas': 0
        }

    def save(self, **kwargs):
        return super().save(**kwargs)
